<?php

namespace App\Model\Attend;

use App\Model\BaseModel;
use EasySwoole\Component\Di;
use EasySwoole\Http\Message\Status;
use App\Model\Attend\Group as GroupModel;
use App\Model\Attend\Calendar as CalendarModel;

/**
 * 
 * Class Index. 
 * @package App\HttpController
 */
class Calendar extends BaseModel
{

    public function index() {

    }

    /**
     * 获取权限列表
     * @return [type] [description]
     */
    public function group(){

        try {
            $groupObj = new GroupModel();
            $field = 'id,name';
            $where = [];
            $where['companyid'] = $this->userinfo['companyid'];
            $where['type'] = 1;
            $data = $groupObj->getAll($field,$where);
            
        }catch(\Exception $e) {
            // $e->getMessage();
            return $this->writeJson(Status::CODE_BAD_REQUEST, "Server Error");
        }

        return $this->writeJson(Status::CODE_LIST_OK, "ok", $data); 
    }

    /**
     * 查询所有特殊需要上班，不需要上班的日期，每周需要上班的日期
     * @return [type] [description]
     */
    public function list(){

        if(!isset($this->params['groupid']) || empty($this->params['groupid'])){
            return $this->writeJson(Status::CODE_BAD_REQUEST, "传入参数有误", []); 
        }
        $groupid = $this->params['groupid'];
        if(isset($this->params['year']) && !empty($this->params['year'])){
            $year = $this->params['year'];
        }else{
            $year = date('Y',time());
        }
        
        try {
            $groupObj = new GroupModel();
            $calendarObj = new CalendarModel();
            //查询该组每周上班星期
            $field = 'id,week';
            $where = [];
            $where['id'] = $groupid;
            $week = $groupObj->getOne($field,$where);

            $first = strtotime("{$year}-01-01");
            $last = strtotime("{$year}-12-31");
            $cal_where = [];
            $cal_where['attendid'] = $groupid;
            $cal_where['time'] = [[$first, $last], 'BETWEEN'];
            $cal_where['status'] = 1;
            //查询该组特殊上班日期，年为单位
            $list = $calendarObj->getAll('id,time,rest',$cal_where);
            $on_work_day = [];
            $str_active_day = [];
            foreach ($list as $key => $value) {
                if($value['rest'] == 1){
                    $on_work_day[] = date('Y-m-d',$value['time']);
                }elseif($value['rest'] == 2){
                    $str_active_day[] = date('Y-m-d',$value['time']);
                }
            }

            return $this->writeJson(Status::CODE_OK, "ok", ['week'=>$week['week'],'on_work_day'=>$on_work_day,'str_active_day'=>$str_active_day]); 
        }catch(\Exception $e){
            return $this->writeJson(Status::CODE_BAD_REQUEST, "Server Error");
        }
    }

    /**
     * 设置日历中的特殊日期
     */
    public function set(){
        // $data = $this->params;
        
        $data['time'] = $this->params['time'];
        $data['attendid'] = $this->params['groupid'];
        $data['rest'] = $this->params['rest'];

        try {
            //时间戳数据
            $intime = strtotime($data['time']);
            $w = date('w', $intime);

            $calendarObj = new CalendarModel();
            $groupObj = new GroupModel();

            //获取考勤组的正常考勤日期
            $week = $groupObj->getOne('week',['id'=>$data['attendid']]);

            $where = [];
            $where['attendid'] = $data['attendid'];
            $where['time'] = $intime;
            //查询之前是否已经设置
            $isset = $calendarObj->getOne('id,rest',$where);

            //1 添加，2修改
            $addOrUpdate = 1;
            $data['time'] = $intime;
            if($isset){
                //存在进行更新
                $addOrUpdate = 2;

                if($data['rest'] != $isset['rest']){
                    //下面进行修改
                }else{
                    //无需修改
                    $addOrUpdate = 3;
                }
                
                $data['updatetime'] = time();
            }else{
                
                //添加
                $data['createtime'] = time();
                $data['status'] = 1;

                if(strstr($week['week'],$w)){
                    //在上班日
                    if($data['rest'] != 1){
                        //在下面插入数据
                    }else{
                        $addOrUpdate = 3;
                    }
                }else{
                    //不在上班日
                    if($data['rest'] != 2){
                        //在下面插入数据
                    }else{
                        $addOrUpdate = 3;
                    }
                }
            }


            //进行修改或插入数据
            if($addOrUpdate == 1){
                $result = $calendarObj->add($data);
            }elseif($addOrUpdate == 2){
                $up_where = [];
                $up_where['id'] = $isset['id'];
                $result = $calendarObj->update($data, $up_where);
            }else{
                $result = true;
            }


        }catch(\Exception $e) {
            echo $e->getMessage();
            return $this->writeJson(Status::CODE_BAD_REQUEST, "Server Error");
        }
       
        if($result) {
            return $this->writeJson(Status::CODE_OK, 'OK', []);
        } else {
            return $this->writeJson(Status::CODE_BAD_REQUEST, '操作数据失败', ['id' => 0 ]);
        }
    }

    
}
